package evolution;

import java.io.File;
import java.io.IOException;

import exceptions.IncoherentStateException;

import polylegs.Environnement;
import genome.Genome;
import gui_interface.Config;

public class Evolution {

	public Config config;
	public Genome genome;

	public Evolution(String configFile, String Initrules, String folder,boolean randomInit,
			String ruletype) {
		config = new Config(configFile);
		System.out.println("size of the environment : "
				+ config.size_environment);
		System.out.println("x init : " + config.x_init);
		System.out.println("y init : " + config.y_init);
		if (randomInit) {
			genome = new Genome(ruletype,true);
		} else {
			genome = new Genome(Initrules);
		}
		System.out.println(genome.toString());
		new File(folder).mkdir();
		new File(folder + "/dessin").mkdir();
		new File(folder + "/actions").mkdir();
		new File(folder + "/Rules").mkdir();

		for (int i = 0; i < config.number_of_mutations_per_evolution_process; i++) {
			for (int j = 0; j < config.number_of_tests_per_mutation + 0; j++) {
				Environnement env;
				try {
					env = new Environnement(config, genome, folder + "/movie/"
							+ i + j + ".xml", folder + "/actions/" + i + j
							+ ".xml", folder + "/dessin/" + i + j + ".png");
					try {
						env.create_shape();
						genome.write(folder + "/Rules/rules" + i + j + ".txt");
					} catch (IncoherentStateException e) {
					}
					
				} catch (IOException e) {
					System.out.println("fichiers introuvables");
					e.printStackTrace();
				}

			}

		}

	}
	public Evolution(String configFile, String Initrules, String folder,boolean randomInit,
			String ruletype, String number) {
		config = new Config(configFile);
		System.out.println("size of the environment : "
				+ config.size_environment);
		System.out.println("x init : " + config.x_init);
		System.out.println("y init : " + config.y_init);
		if (randomInit) {
			genome = new Genome(ruletype,true);
		} else {
			genome = new Genome(Initrules);
		}
		System.out.println(genome.toString());
		new File(folder).mkdir();
		new File(folder + "/dessin").mkdir();
		new File(folder + "/actions").mkdir();
		new File(folder + "/Rules").mkdir();

		
				Environnement env;
				try {
					env = new Environnement(config, genome, folder + "/movie/"
							+ number + ".xml", folder + "/actions/" + number
							+ ".xml", folder + "/dessin/" + number + ".png");
					try {
						env.create_shape();
						genome.write(folder + "/Rules/rules" + number + ".txt");
					} catch (IncoherentStateException e) {
						
					}
					
				} catch (IOException e) {
					System.out.println("fichiers introuvables");
					e.printStackTrace();
				}

		

	}
	
	public void evolve() {

	}
}
